#!/usr/bin/env python
# encoding: utf-8
"""
# @Time    : 2022/6/14 11:26
# @Author  : 心如潭水静无风
# @Email   : 3148508410@qq.com
# @File    : 疫苗管理系统.py
# @Software: PyCharm
"""

import pymysql


class Vaccines(object):
    def __init__(self):
        self.db = pymysql.connect(host="localhost", port=3306,
                                  user="root", password="root",
                                  database="vaccine", charset="utf8")
        self.cur = self.db.cursor()
        self.state = False  # 判断是否登录

    def register(self):
        name = input("请输入你的用户名：")
        passwd = input("请输入你的密码：")
        sql = "insert into user values(%s,%s)"
        try:
            self.cur.execute(sql, [name, passwd])
            self.db.commit()
            print("注册成功")
        except Exception as e:
            print("错误: ", e, "(用户名可能已经存在)")
            self.db.rollback()
        self.close_all()

    def login(self):
        name = input("请输入你的用户名: ")
        passwd = input("请输入你的密码: ")
        try:
            sql = "select *  from user where name = '%s'" % name
            self.cur.execute(sql)
            data = self.cur.fetchone()
            if data[1] == passwd:
                print("登录成功！！！")
                self.state = True
            else:
                print("密码错误！！！")
        except:
            print("用户名错误！！！")
        # self.close_all()

    def adds(self):
        if self.state:
            v_type = input("疫苗类型：")
            v_num = input("疫苗数量：")
            v_price = input("疫苗价格：")
            sql = "insert into vaccine_message(vaccine_type, vaccine_num, vaccine_price) " \
                  "value ('%s', '%s', '%s')" % (v_type, v_num, v_price)
            self.cur.execute(sql)
            self.db.commit()
            print("插入成功！")
            return "ok"
        else:
            print("请登录！！！")
            self.login()

    def check(self):  # 查询
        if self.state:
            vid = int(input('请输入您要修改的id序号：'))
            sql = "select * from vaccine_message where vaccine_id={}".format(vid)
            self.cur.execute(sql)
            self.db.commit()
            result = self.cur.fetchall()
            for i in result:
                print("id:{}, 名称:{}, 数量:{}, 价格:{}".format(i[0], i[1], i[2], i[3]))
            print("查询成功！")
            return "ok"
        else:
            print("请登录！！！")
            self.login()

    def update(self):  # 修改
        if self.state:
            vid = int(input('请输入您要修改的id序号：'))
            v_name = input("请输入修改后的疫苗名称：")
            v_num = input("请输入修改后的疫苗数量：")
            v_price = input("请输入修改后的疫苗价格：")
            sql = "update vaccine_message set vaccine_type='{}' where vaccine_id={}".format(v_name, vid)
            sql1 = "update vaccine_message set vaccine_num='{}' where vaccine_id={}".format(v_num, vid)
            sql2 = "update vaccine_message set vaccine_price='{}' where vaccine_id={}".format(v_price, vid)
            self.cur.execute(sql)
            self.cur.execute(sql1)
            self.cur.execute(sql2)
            self.db.commit()
            print('更新成功！')
        else:
            print("请登录！！！")
            self.login()

    def delete(self):
        v_id = int(input("请输入你要删除的id号："))
        if self.state:
            sql = "delete from vaccine_message where vaccine_id={}".format(v_id)
            self.cur.execute(sql)
            self.db.commit()
            print('删除{}号成功！'.format(v_id))
        else:
            print("请登录！！！")
            self.login()

    def show(self):
        if self.state:
            sql = "select * from vaccine_message"
            self.cur.execute(sql)
            self.db.commit()
            result = self.cur.fetchall()
            for i in result:
                print("id:{}, 名称:{}, 数量:{}, 价格:{}".format(i[0], i[1], i[2], i[3]))
            print("查询成功！")
            return "ok"
        else:
            print("请登录！！！")
            self.login()

    def close_all(self):
        self.cur.close()
        self.db.close()


if __name__ == '__main__':
    vaccine = Vaccines()
    while True:
        print("*****************菜单*****************")
        print("1.注册                2.登录")
        print("3.添加疫苗信息         4.查询疫苗信息")
        print("5.更新疫苗信息         6.删除疫苗信息")
        print("7.显示所有信息         8.退出")
        a = input("请选择你要操作的序号:  ")
        if a == "1":  # 注册
            vaccine.register()
        elif a == "2":  # 登录
            vaccine.login()
        elif a == '3':  # 添加疫苗信息
            vaccine.adds()
        elif a == '4':  # 查询疫苗信息
            vaccine.check()
        elif a == '5':  # 更新疫苗信息
            vaccine.update()
        elif a == '6':  # 删除疫苗信息
            vaccine.delete()
        elif a == '7':  # 显示全部信息
            vaccine.show()
        elif a == '8':
            break
